﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Gigamud.LuaEngine;

namespace Gigamud
{
    public partial class GigaMud : Form
    {

        private bool _ignoreNextEcho;
        private string _lastCommand;
        private string _lastLine;
        private LuaEngine.LuaEngine _luaEngine;
        private ConnectionController _connControl;

        public GigaMud()
        {
            InitializeComponent();
            _lastCommand = String.Empty;
            _lastLine = String.Empty;
            _ignoreNextEcho = false;
            _connControl = new ConnectionController(this.TermCtrl);
            _luaEngine = new LuaEngine.LuaEngine(_connControl);
        }

        private void cmdConnect_Click(object sender, EventArgs e)
        {
            /*this.TermCtrl.Host = this.txtHost.Text;
            this.TermCtrl.Method = WalburySoftware.ConnectionMethod.Telnet;

            this.TermCtrl.Connect();

            this.TermCtrl.SetPaneColors(Color.Gray, Color.Black);
            this.TermCtrl.Focus();
            this.TermCtrl.OnDisconnect += new EventHandler<WalburySoftware.TerminalControl.TerminalEventArgs>(TermCtrl_OnDisconnect);
            this.TermCtrl.OnUpdate += new EventHandler<WalburySoftware.TerminalControl.TerminalEventArgs>(TermCtrl_OnUpdate);
            this.TermCtrl.OnNewChar += new EventHandler<WalburySoftware.TerminalControl.TerminalEventArgs>(TermCtrl_OnNewChar);*/

            _connControl.Connect(this.txtHost.Text);
            _luaEngine.Initialize();
        }

        private void cmdDisconnect_Click(object sender, EventArgs e)
        {
            _connControl.Disconnect("Disconnected by GUI.");
        }

        private void cmdReloadLua_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Reloading Lua VM.");
            _luaEngine.Reload();
            //_luaEngine.Cleanse();
            //_luaEngine = new LuaEngine.LuaEngine(_connControl);
        }
/*
        private void TermCtrl_OnDisconnect(object sender, WalburySoftware.TerminalControl.TerminalEventArgs args)
        {
            string msg = new String(args.MessageOne);
            //MessageBox.Show(msg);
        }

        private void TermCtrl_OnNewChar(object sender, WalburySoftware.TerminalControl.TerminalEventArgs args)
        {
            char c = args.MessageOne[0];
            if (c != '\r' || c != '\n')
            {
                _lastLine += new string(args.MessageOne);
            }
            else
            {
                if (_lastLine.Contains("Also here: giant rat"))
                {
                    TermCtrl.SendText("attack giant rat\r");
//                    MessageBox.Show(_lastLine + "|");
                }
                _lastLine = String.Empty;
            }
        }

        private void TermCtrl_OnUpdate(object sender, WalburySoftware.TerminalControl.TerminalEventArgs args)
        {
            string msg = new string(args.MessageOne);
            if (msg != _lastLine + _lastCommand)
            {
                MessageBox.Show(msg);
                if (msg.Contains("type \"new\""))
                {
                    _lastLine = msg;
                    _lastCommand = "iorek";
                    this.TermCtrl.SendText(_lastCommand + "\r");
                }
                else if (msg.Contains("password"))
                {
                    _lastLine = msg;
                    _lastCommand = "open";
                    this.TermCtrl.SendText(_lastCommand + "\r");
                }
                else if (msg.Contains("(N)onstop, (Q), or (C)ontinue?"))
                {
                    _lastLine = msg;
                    _lastCommand = "n";
                    this.TermCtrl.SendText(_lastCommand);
                }
            }

            //MessageBox.Show(msg + " : " + Convert.ToString(_ignoreNextEcho));
            if (!_ignoreNextEcho)
            {
                if (msg.Contains("Also here: giant rat"))
                {
                    _lastLine = msg;
                    _lastCommand = "attack giant rat";
                    this.TermCtrl.SendText(_lastCommand + "\r");
                    _ignoreNextEcho = true;
                }
                else if (msg.Contains("Enter your password:"))
                {
                    _lastLine = msg;
                    _lastCommand = "open";
                    this.TermCtrl.SendText(_lastCommand + "\r");
                    _ignoreNextEcho = true;
                }
                else if (msg.Contains("(N)onstop, (Q), or (C)ontinue?"))
                {
                    _lastLine = msg;
                    _lastCommand = "n";
                    this.TermCtrl.SendText(_lastCommand);
                    _ignoreNextEcho = true;
                }
            }
            else
            {
                _ignoreNextEcho = false;
            }

        }
*/
/*        private class EventHandler
        {
            private WalburySoftware.TerminalControl _tc;

            public EventHandler(WalburySoftware.TerminalControl tc)
            {
                _tc = tc;
                //_tc.OnDisconnect += new WalburySoftware.TerminalControl.ConnectionTag.DEventHandler(OnDisconnect);
                //_tc.OnUpdate += new WalburySoftware.TerminalControl.DEventHandler(OnUpdate);
            }

            private void OnDisconnect(object sender)
            {
            }
            private void OnUpdate(object sender)
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\testlog.txt", true))
                {
                    file.WriteLine(_tc.GetLastLine());
                }

            }
        }*/

    }
}
